package com.yyplatform.admin.platform.role.service.impl;

import java.util.List;

import com.yyplatform.admin.platform.role.model.entity.SysUserPostEntity;
import com.yyplatform.admin.platform.role.repository.SysUserPostRepository;
import com.yyplatform.admin.infra.exception.ErrorCode;
import com.yyplatform.admin.infra.exception.ServiceException;

import org.modelmapper.ModelMapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;

import com.yyplatform.admin.platform.role.model.dto.SysPostDTO;
import com.yyplatform.admin.platform.role.model.dto.SysPostFormDTO;
import com.yyplatform.admin.platform.role.model.dto.SysPostQueryDTO;
import com.yyplatform.admin.platform.role.model.entity.SysPostEntity;
import com.yyplatform.admin.platform.role.repository.SysPostRepository;
import com.yyplatform.admin.platform.role.service.SysPostService;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * Created on 2024-09-14
 */
@Slf4j
@AllArgsConstructor
@Service
public class SysPostServiceImpl implements SysPostService {

    private SysPostRepository sysPostRepository;
    private SysUserPostRepository sysUserPostRepository;
    private ModelMapper modelMapper;


    @Override
    public Page<SysPostDTO> query(SysPostQueryDTO pageQueryDTO) {
        Page<SysPostEntity> entityPage =
                sysPostRepository.findPage(pageQueryDTO.getName(),
                        pageQueryDTO.getCode(),
                        pageQueryDTO.getEnabled(),
                        pageQueryDTO.getPageRequestOrderByIdDesc());

        List<SysPostDTO> list = entityPage.stream()
                .map(e -> modelMapper.map(e, SysPostDTO.class))
                .toList();

        return new PageImpl<>(list, entityPage.getPageable(), entityPage.getTotalElements());

    }

    @Override
    public SysPostDTO get(Long id) {
        SysPostEntity entity = sysPostRepository.getReferenceById(id);
        return modelMapper.map(entity, SysPostDTO.class);
    }

    @Override
    public void save(SysPostFormDTO sysDeptFormDTO) {
        SysPostEntity entity = modelMapper.map(sysDeptFormDTO, SysPostEntity.class);
        sysPostRepository.save(entity);
    }

    @Override
    public void update(SysPostFormDTO sysDeptFormDTO) {
        SysPostEntity entity = modelMapper.map(sysDeptFormDTO, SysPostEntity.class);
        SysPostEntity oldEntity = sysPostRepository.getReferenceById(sysDeptFormDTO.getId());
        // todo
    }

    @Override
    public void delete(Long id) {
        List<SysUserPostEntity> sysUserPostEntities = sysUserPostRepository.findByPostId(id);
        if (sysUserPostEntities != null && sysUserPostEntities.size() > 0) {
            throw ServiceException.of(ErrorCode.DELETE_DATA_ERROR_IN_USE);
        }

        sysPostRepository.deleteById(id);
    }

    @Override
    public List<SysPostDTO> getOptions() {
        List<SysPostEntity> list = sysPostRepository.findAll();
        return list.stream().map(e -> modelMapper.map(e, SysPostDTO.class)).toList();
    }

}
